﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.System.Flow;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.System.Flow;
using PmSoft.Utilities;

namespace ManagementApi.Controllers.SystemManage
{
    /// <summary>
    /// 流程接口
    /// </summary>
    [Route("/api/v1/flow")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class FlowController : Controller
    {
        private readonly SystemUserApplicationContext applicationContext;
        private readonly SysFlowService sysFlowService;
        private readonly SysFlowEngineService sysFlowEngineService;
        private readonly IMapper mapper;

        public FlowController(SystemUserApplicationContext applicationContext, SysFlowEngineService sysFlowEngineService, SysFlowService sysFlowService, IMapper mapper)
        {
            this.applicationContext = applicationContext;
            this.sysFlowService = sysFlowService;
            this.sysFlowEngineService = sysFlowEngineService;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取流程列表(分页)
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<FlowDto>> GetFlows([FromQuery] FlowQuery query, int pageNum = 1, int pageSize = 20)
        {
            var flows = sysFlowService.GetFlows(query, pageNum, pageSize);
            List<int> flowIds = flows.Select(a => a.Id).ToList();
            IEnumerable<SysFlowEngine> sysFlowEngines = sysFlowEngineService.GetFlowEnginesByFlowIds(flowIds);
            var dtos = flows.AsPageDto(source => { return source.AsDto(sysFlowEngines); });
            return ApiResult<PageDto<FlowDto>>.Success(dtos);
        }

        /// <summary>
        /// 创建流程
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("create")]
        [PermissionCheck("sys:flow:add")]
        public ApiResult Add([FromBody] FlowEditForm form)
        {
            SysFlow flow = new SysFlow();
            mapper.Map(form, flow);
            flow.CreateTime = DateTime.Now;
            sysFlowService.CreateFlow(flow);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// 流程表单数据
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        public ApiResult<FlowEditForm> GetRole(int id)
        {
            var flow = sysFlowService.GetFlow(id);
            if (flow == null)
                return ApiResult<FlowEditForm>.Failure("流程数据不存在");
            FlowEditForm flowEditForm = new FlowEditForm();
            mapper.Map(flow, flowEditForm);
            return ApiResult<FlowEditForm>.Success(flowEditForm);
        }

        /// <summary>
        /// 修改流程
        /// </summary>
        /// <param name="id"></param>
        /// <param name="flowEditForm"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [PermissionCheck("sys:flow:edit")]
        public ApiResult Update(int id, [FromBody] FlowEditForm flowEditForm)
        {
            var flow = sysFlowService.GetFlow(id);
            if (flow == null) return ApiResult.Failure("流程不存在");
            mapper.Map(flowEditForm, flow);
            sysFlowService.UpdateFlow(flow);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 流程步骤详情表单
        /// </summary>
        /// <param name="flowId">流程Id</param>
        /// <returns></returns>
        [HttpGet("{flowId}")]
        [PermissionCheck("sys:flowengine:edit")]
        public ApiResult<IEnumerable<FlowEngineEditForm>> GetFlowEngines(int flowId)
        {
            var flowEngineEditForms = sysFlowEngineService.GetSysFlowEngineByFlowId(flowId);
            List<FlowEngineEditForm> retDto = new List<FlowEngineEditForm>();
            mapper.Map(flowEngineEditForms, retDto);
            return ApiResult<IEnumerable<FlowEngineEditForm>>.Success(retDto);
        }

        /// <summary>
        /// 删除流程
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [PermissionCheck("sys:flow:delete")]
        public ApiResult Delete(string ids)
        {
            sysFlowService.DeleteFlows(ValueUtility.ConvertToList<int>(ids));
            return ApiResult.Success("删除成功");
        }

        /// <summary>
        /// 新增流程步骤
        /// </summary>
        /// <param name="flowId"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("flowEngineAdd/{flowId}")]
        [PermissionCheck("sys:flowengine:add")]
        public ApiResult AddFlowEngine(int flowId, IEnumerable<FlowEngineEditForm> form)
        {
            List<SysFlowEngine> sysFlowEngines = new List<SysFlowEngine>();
            mapper.Map(form, sysFlowEngines);
            sysFlowEngineService.InsertSysFlowEngines(flowId, sysFlowEngines);
            return ApiResult.Success("操作成功");
        }
    }
}